package com.mdd.admin.service.order.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mdd.admin.service.order.IDeliveryInfoService;
import com.mdd.admin.validate.order.DeliveryInfoCreateValidate;
import com.mdd.admin.validate.order.DeliveryInfoSearchValidate;
import com.mdd.admin.validate.order.DeliveryInfoUpdateValidate;
import com.mdd.admin.vo.order.DeliveryInfoDetailVo;
import com.mdd.admin.vo.order.DeliveryInfoListedVo;
import com.mdd.common.base.page.PageValidate;
import com.mdd.common.core.PageResult;
import com.mdd.common.entity.order.DeliveryInfo;
import com.mdd.common.mapper.order.DeliveryInfoMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;
import javax.annotation.Resource;
import java.util.LinkedList;
import java.util.List;

/**
 * 租赁交易信息实现类
 * @author LikeAdmin
 */
@Service
public class DeliveryInfoServiceImpl implements IDeliveryInfoService {

    @Resource
    DeliveryInfoMapper orderDeliveryInfoMapper;

    /**
     * 租赁交易信息列表
     *
     * @author LikeAdmin
     * @param pageValidate 分页参数
     * @param searchValidate 搜索参数
     * @return PageResult<OrderDeliveryInfoListedVo>
     */
    @Override
    public PageResult<DeliveryInfoListedVo> list(PageValidate pageValidate, DeliveryInfoSearchValidate searchValidate) {
        Integer page  = pageValidate.getPageNo();
        Integer limit = pageValidate.getPageSize();

        QueryWrapper<DeliveryInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("id");

        orderDeliveryInfoMapper.setSearch(queryWrapper, searchValidate, new String[]{
            "=:orderNo@order_no:str",
            "=:type:str",
            "=:videoUrls@video_urls:str",
            "=:imgUrls@img_urls:str"
        });

        IPage<DeliveryInfo> iPage = orderDeliveryInfoMapper.selectPage(new Page<>(page, limit), queryWrapper);

        List<DeliveryInfoListedVo> list = new LinkedList<>();
        for(DeliveryInfo item : iPage.getRecords()) {
            DeliveryInfoListedVo vo = new DeliveryInfoListedVo();
            BeanUtils.copyProperties(item, vo);
            list.add(vo);
        }

        return PageResult.iPageHandle(iPage.getTotal(), iPage.getCurrent(), iPage.getSize(), list);
    }

    /**
     * 租赁交易信息详情
     *
     * @author LikeAdmin
     * @param id 主键参数
     * @return OrderDeliveryInfo
     */
    @Override
    public DeliveryInfoDetailVo detail(Long id) {
        DeliveryInfo model = orderDeliveryInfoMapper.selectById(id);

        Assert.notNull(model, "数据不存在");

        DeliveryInfoDetailVo vo = new DeliveryInfoDetailVo();
        BeanUtils.copyProperties(model, vo);
        return vo;
    }

    /**
     * 租赁交易信息新增
     *
     * @author LikeAdmin
     * @param createValidate 参数
     */
    @Transactional
    @Override
    public void add(DeliveryInfoCreateValidate createValidate) {
        DeliveryInfo model = new DeliveryInfo();
        model.setOrderNo(createValidate.getOrderNo());
        model.setType(createValidate.getType());
        model.setVideoUrls(createValidate.getVideoUrls());
        model.setImgUrls(createValidate.getImgUrls());
        orderDeliveryInfoMapper.insert(model);
    }

    /**
     * 租赁交易信息编辑
     *
     * @author LikeAdmin
     * @param updateValidate 参数
     */
    @Transactional
    @Override
    public void edit(DeliveryInfoUpdateValidate updateValidate) {
        DeliveryInfo model = orderDeliveryInfoMapper.selectOne(
                new QueryWrapper<DeliveryInfo>()
                    .eq("id",  updateValidate.getId())
                    .last("limit 1"));

        Assert.notNull(model, "租赁交易信息编辑数据不存在!");

        model.setId(updateValidate.getId());
        model.setOrderNo(updateValidate.getOrderNo());
        model.setType(updateValidate.getType());
        model.setVideoUrls(updateValidate.getVideoUrls());
        model.setImgUrls(updateValidate.getImgUrls());
        orderDeliveryInfoMapper.updateById(model);
    }

    /**
     * 租赁交易信息删除
     *
     * @author LikeAdmin
     * @param id 主键ID
     */
    @Transactional
    @Override
    public void del(Long id) {
        DeliveryInfo model = orderDeliveryInfoMapper.selectById(id);

        Assert.notNull(model, "租赁交易信息数据不存在!");

        orderDeliveryInfoMapper.deleteById(id);
    }

}
